package com.woniuxy.microsystemuapserver.mapper;

import com.slsupermarket.common.bean.StoreBean;
import com.woniuxy.microsystemuapserver.bean.ChildBean;
import com.woniuxy.microsystemuapserver.bean.MenuBean;
import com.woniuxy.microsystemuapserver.bean.PageResult;
import com.woniuxy.microsystemuapserver.bean.RoleBean;
import com.woniuxy.microsystemuapserver.bean.UserBean;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.mapping.FetchType;
import org.yaml.snakeyaml.events.Event;

import java.util.List;
import java.util.Map;


/**
 * @Author: huhenggg
 * @Date: 2021/1/25 0025 下午 4:33
 */
@Mapper
public interface UserMapper {

    /**
     * 查询所有的用户
     * @param
     * @param userBean
     * @return
     */
     List<UserBean> findAll(@Param("uu") UserBean userBean);


    /**
     * 修改用户信息
     * @param userBean
     * @return
     */
     int  updateUserBean(@Param("uu")UserBean userBean);


    /**
     * 新增用户
     * @param userBean
     * @return
     */

    @Insert("INSERT INTO `user_info`" +
            "(`user_account`,`password`,`user_number`,`user_name`," +
            "`fk_dept_id`,`fk_role_id`,`job`,`telephone`,`email`,`gender`," +
            "`id_card`,`adress`,`parent_id`,`login_nums`,`status`,`create_time`)" +
            "VALUES " +
            "( #{us.userAccount}, #{us.password}, #{us.userNumber}, #{us.userName}," +
            "#{us.deptId}, #{us.rid}, #{us.job}, #{us.telephone},#{us.email}," +
            " #{us.gender}, #{us.idCard},#{us.adress}, #{us.parentId},0,1,NOW())")
     int addUser(@Param("us")UserBean userBean);


    /**
     * 根据id查询用户信息
     * @param userId
     * @return
     */
    @Select(" SELECT  ui.`id`  ,ui.`user_number` AS userNumber, ui.user_name AS userName, di.`dept_name` AS deptName," +
            " ui.`job`,ui.`telephone`,ui.`email`,ui.`gender`," +
            " ui.`id_card` AS idCard,ui.`adress`,ui.`status`" +
            " FROM `user_info` AS ui ,`dept_info` AS di" +
            " WHERE ui.`fk_dept_id` = di.id AND ui.id = #{userId}")
    @Results({
            @Result(id = true,column = "id",property = "id"),
            @Result(property = "storeBeans",column = "id",many = @Many(select = "findStoreById",fetchType = FetchType.LAZY))
    })
    Map findAUser(Long userId);

    @Select(" SELECT tsi.`store_name` as storeName FROM " +
            " `t_store_user_info` AS tsui ,`t_store_info` AS  tsi " +
            " WHERE  tsui.`fk_user_id` = #{uid} AND tsui.`fk_store_id` = tsi.`id` ")
    List<Map> findStoreById(int uid);


    /**
     * 新增老板账号
     * @param userBean
     * @return
     */
    @Insert("INSERT INTO `user_info`" +
            "(`user_account`,`password`,`user_number`,`user_name`," +
            "`fk_dept_id`,`fk_role_id`,`job`,`telephone`,`email`,`gender`," +
            "`id_card`,`adress`,`parent_id`,`login_nums`,`status`,`create_time`)" +
            "VALUES " +
            "( #{us.userAccount}, #{us.password}, #{us.userNumber}, #{us.userName}," +
            "#{us.deptId}, 2, #{us.job}, #{us.telephone},#{us.email}," +
            " #{us.gender}, #{us.idCard},#{us.adress}, 0,0,1,NOW())")
    int addBossUser(@Param("us")UserBean userBean);


    @Select("SELECT `fk_role_id` as roleId FROM `user_info` WHERE `user_account`= #{userAccount};")
    @Results({
            @Result(id = true,property = "id",column = "id"),
            @Result(property = "roleBean",column = "roleId",one = @One(select = "findRoleById",fetchType = FetchType.LAZY))
    })
    UserBean findRoleAndPermission(String userAccount);


    @Select("SELECT `id`,`role_desc` as roleDesc FROM `role_info` WHERE id = #{roleId};")
    @Results({
            @Result(id = true,property = "id",column = "id"),
            @Result(property="menuBeans",column = "id",many = @Many(select = "findMenuId",fetchType = FetchType.LAZY))
    })
    RoleBean findRoleById(int roleId);

    @Select("SELECT mi.`id`,mi.`menu_name` as menuName,mi.`menu_desc` as menuDesc,mi.`fk_parent_id`,rm.`opt_perm`as optPerm,mi.menu_level as menuLevel,mi.icon FROM `menu_info` AS mi,`role_menu` AS rm WHERE rm.`fk_menu_id`= mi.`id` AND rm.`fk_role_id` = #{id}")
    @Results(value = {
            @Result(id = true,property = "id",column = "id"),
            @Result(property = "children",column = "id",many = @Many(select = "findall",fetchType = FetchType.LAZY))
    })
    List<MenuBean> findMenuId(int id);

    @Select("SELECT id,`menu_desc` as menuDesc , url, icon FROM `menu_info` WHERE `fk_parent_id` = #{id}")
    List<ChildBean> findall(int id);


    /**
     * 登录，根据用户名查询密码
     * @param loginName
     * @return
     */
    @Select("SELECT id,`user_account` AS userName,`password`FROM `user_info`" +
            "WHERE `user_account` = #{loginName}")
    UserBean login(@Param("loginName") String loginName);





}
